package com.chixing.controller;

import com.chixing.pojo.Myorder;
import com.chixing.service.*;
import com.chixing.service.impl.MsgSender;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

import com.chixing.util.DateUtil;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/10/19:48
 * @Description:
 */
@Controller
public class OrderController {
    @Autowired
    private RentalCarService rentalCarService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private MyCouponService myCouponService;
    @Autowired
    private MsgSender msgSender;

    //个人中心订单
    @GetMapping(value = {"myorder/{custId}", "myorder/{custId}/{status}"})
    public ModelAndView getOrderCustomer(
            @PathVariable("custId") Integer custId,
            @PathVariable(value = "status", required = false) Integer status) {
        ServerResponse serverResponse = orderService.getByOrderCustomer(custId, status);
        ModelAndView mav = new ModelAndView();
        if (serverResponse.getCode() == 200) {
            mav.addObject("serverResponse", serverResponse);
        } else {
            mav.addObject("message", "无数据");
            mav.setViewName("center/myorder");
        }
        return mav;
    }

    //预定
    @GetMapping("myOrder")
    public ModelAndView getInstance(Integer custId,
                                    Integer carId,
                                    Integer rentStoreId,
                                    Integer giveStoreId,
                                    String borrowCarDate,
                                    String retCarDate
    ) {
        System.out.println(carId);
        System.out.println(custId);
        ServerResponse mycoupons = myCouponService.getMyCoupons(custId);
        ServerResponse carDetail = rentalCarService.getCarDetail(carId);
        ServerResponse borrowStore = storeService.getById(rentStoreId);
        ServerResponse returnStore = storeService.getById(giveStoreId);
        Date getCarDate = DateUtil.stringToDate(borrowCarDate, "yyyy-MM-dd");
        Date renturnCarDate = DateUtil.stringToDate(retCarDate, "yyyy-MM-dd");
        ModelAndView mav = new ModelAndView();
        mav.addObject("custId", custId);
        mav.addObject("carId", carId);
        mav.addObject("rentStoreId", rentStoreId);
        mav.addObject("giveStoreId", giveStoreId);
        mav.addObject("borrowCarDate", borrowCarDate);
        mav.addObject("retCarDate", retCarDate);
        mav.addObject("mycoupons", mycoupons);
        mav.addObject("carDetail", carDetail);
        mav.addObject("borrowStore", borrowStore);
        mav.addObject("returnStore", returnStore);
        mav.addObject("borrowCarDate", borrowCarDate);
        mav.addObject("retCarDate", retCarDate);
        mav.addObject("dayNum", (renturnCarDate.getTime() - getCarDate.getTime()) / (1000 * 60 * 60 * 24));
        mav.setViewName("myorder/save");
        return mav;

    }

    //数据绑定到订单填写页面
    @GetMapping("myOrder/{orderId}")
    public ModelAndView getOrderCarAndStore(@PathVariable("orderId") Integer orderId) {
        ServerResponse serverResponse = orderService.getOrderCarAndStore(orderId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse", serverResponse);
        mav.setViewName("myorder/save");
        return mav;

    }

//    //确认订单
//    @PostMapping("saveOrder")
//    public ModelAndView addOrder(Integer custId,
//                                 Integer carId,
//                                 Integer rentStoreId,
//                                 Integer giveStoreId,
//                                 String borrowCarDate,
//                                 String retCarDate,
//                                 String carRentName,
//                                 String carRentIdCard,
//                                 Long carTelno,
//                                 Integer rentDays,
//                                 Double orderPrice) {
//        ModelAndView mav = new ModelAndView();
//        mav.addObject("custId",custId);
//        mav.addObject("carId", carId);
//        mav.addObject("rentStoreId", rentStoreId);
//
//        mav.addObject("giveStoreId", giveStoreId);
//
//        mav.addObject("borrowCarDate", borrowCarDate);
//
//        mav.addObject("retCarDate", retCarDate);
//        mav.addObject("carRentName", carRentName);
//        mav.addObject("carRentIdCard", carRentIdCard);
//        mav.addObject("carTelno", carTelno);
//
//        mav.addObject("rentDays", rentDays);
//
//        mav.addObject("orderPrice", orderPrice);
//
//        mav.setViewName("myorder/pay");
//        return mav;
//
//    }


    //填写订单提交订单
    @PostMapping("creatOrder")
    public ModelAndView createOrder(Integer custId,
                                    Integer carId,
                                    String borrowCarDate,
                                    String retCarDate,
                                    Integer rentStoreId,
                                    Integer giveStoreId,
                                    String carRentName,
                                    @RequestParam(required = false) Integer couponId,
                                    String carRentIdCard,
                                    Long carTelno,
                                    Double orderPrice,
                                    Integer rentCarDays

    ) {
        System.out.println("sdfsdfsdf");
        //利用UUID生成订单号
        String[] chars = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        StringBuffer buffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int j = 0; j < 12; j++) {
            String str = uuid.substring(j * 2, j * 2 + 2);
            int par = Integer.parseInt(str, 24);
            buffer.append(chars[par % 10]);
        }
        Long orderNum = Long.valueOf(buffer.toString());
        String orderDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));//获取本地时间
        Myorder myOrder = new Myorder();
        myOrder.setCustId(custId);
        myOrder.setCarId(carId);
        myOrder.setOrderNum(orderNum);
        myOrder.setCarRentday(rentCarDays);
        myOrder.setCarRentidcard(carRentIdCard);
        myOrder.setCarRentname(carRentName);
        myOrder.setCarTellno(carTelno);
        myOrder.setCarVin("苏E.666666");
        myOrder.setRentstoreId(rentStoreId);
        myOrder.setGivestoreId(giveStoreId);
        myOrder.setOrderPrice(orderPrice);
        myOrder.setOrderDate(DateUtil.stringToDate(orderDate, "yyyy-MM-dd"));
        myOrder.setOrderStartDate(DateUtil.stringToDate(borrowCarDate, "yyyy-MM-dd"));
        myOrder.setOrderEndDate(DateUtil.stringToDate(retCarDate, "yyyy-MM-dd"));
        myOrder.setLicenseType("c1");
        myOrder.setCarDeposit(rentCarDays * 30.0);
        myOrder.setStatus(1);
        orderService.saveOrder(myOrder);
        msgSender.addPurchaseOrderMsg(orderNum,99);
        Myorder order = orderService.getByOrderNum(orderNum);
        ModelAndView mav = new ModelAndView();
        mav.addObject("order", order);
        mav.setViewName("myorder/pay");
        return mav;
    }


    //支付页面数据绑定
    @GetMapping("creatOrder/{orderId}")
    public ModelAndView getById(@PathVariable("orderId") Integer orderId) {
        ServerResponse serverResponse = orderService.getByOrderId(orderId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse", serverResponse);
        mav.setViewName("myorder/pay");
        return mav;
    }

    //订单详情
    @GetMapping("waitPay/{orderId}")
    public ModelAndView getByOrderId(@PathVariable("orderId") Integer orderId) {
        ServerResponse serverResponse = orderService.getByOrderId(orderId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse", serverResponse);
        mav.setViewName("myorder/waitPay");
        return mav;
    }
    @GetMapping("brandstep/{carId}")
    public ModelAndView getCarById(@PathVariable("carId") Integer carId){
        ServerResponse serverResponse = rentalCarService.getByCarId(carId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("car/brandstep");
        return mav;
    }

}
